package com.thinkgem.dingdan.modules.bu.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.thinkgem.dingdan.common.config.Global;
import com.thinkgem.dingdan.common.persistence.Page;
import com.thinkgem.dingdan.common.utils.IdGen;
import com.thinkgem.dingdan.common.utils.StringUtils;
import com.thinkgem.dingdan.common.utils.excel.ExportToExcelUtil;
import com.thinkgem.dingdan.common.web.BaseController;
import com.thinkgem.dingdan.modules.bu.constant.BusinessConstant;
import com.thinkgem.dingdan.modules.bu.entity.BuOrder;
import com.thinkgem.dingdan.modules.bu.entity.BuOrderCp;
import com.thinkgem.dingdan.modules.bu.entity.BuSalesman;
import com.thinkgem.dingdan.modules.bu.entity.TotalEntity;
import com.thinkgem.dingdan.modules.bu.enumerate.ResultEnum;
import com.thinkgem.dingdan.modules.bu.service.BuOrderCpService;
import com.thinkgem.dingdan.modules.bu.service.BuOrderService;
import com.thinkgem.dingdan.modules.bu.service.BuSalesmanService;
import com.thinkgem.dingdan.modules.bu.service.IBuCustomerService;
import com.thinkgem.dingdan.modules.bu.service.IBuOrderService;
import com.thinkgem.dingdan.modules.bu.service.IBuSupplierService;
import com.thinkgem.dingdan.modules.bu.utils.DateUtil;
import com.thinkgem.dingdan.modules.bu.utils.ExcelTest;
import com.thinkgem.dingdan.modules.bu.utils.OrderStateEnum;
import com.thinkgem.dingdan.modules.bu.wrapper.OrderStateWrapper;
import com.thinkgem.dingdan.modules.bu.wrapper.ParamWrapper;
import com.thinkgem.dingdan.modules.cms.entity.Site;
import com.thinkgem.dingdan.modules.sys.entity.Office;
import com.thinkgem.dingdan.modules.sys.entity.Role;
import com.thinkgem.dingdan.modules.sys.entity.User;
import com.thinkgem.dingdan.modules.sys.service.IUserService;
import com.thinkgem.dingdan.modules.sys.utils.UserUtils;

/**
 * 外部订单Controller
 * 
 * @author
 * @version 2018-08-26
 */
@Controller
@RequestMapping(value = "${adminPath}/bu/buExternalOrder")
public class BuExternalOrderController extends BaseController {

	@Autowired
	private BuOrderService buOrderService;

	@Autowired
	private IBuOrderService iBuOrderService;

	@Resource
	private IBuSupplierService buSupplierService;

	@Resource
	private IBuCustomerService buCustomerService;

	@Resource
	private BuOrderCpService buOrderCpService;

	@Resource
	private IUserService userService;

	@Resource
	private BuSalesmanService buSalesManService;

	@Value("${win.userfiles.basedir}")
	private String winUserfilesBasedir;

	@Value("${linux.userfiles.basedir}")
	private String linuxUserfilesBasedir;
	
	@Value("${aliyunPicPrefixUrl}")
	private String picPrefix;

	@ModelAttribute
	public BuOrder get(@RequestParam(required = false) String id) {
		BuOrder entity = null;
		if (StringUtils.isNotBlank(id)) {
			BuOrder order = new BuOrder();
			order.setId(id);
			order.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
			entity = buOrderService.get(order);
		}
		if (entity == null) {
			entity = new BuOrder();
		}
		return entity;
	}

	/**
	 * 
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * 订单状态(0:待审核;1:待生产;2:生产中;3:待发货;4:待收货;5:待收款;6:已完成)
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = { "list", "" })
	public String list(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes) {
		try {
			
			if(StringUtils.isBlank(buOrder.getSupVal())){
				buOrder.setSupplierId("");
			}
			if(StringUtils.isBlank(buOrder.getCusVal())){
				buOrder.setCustomerId("");
			}
			
			User user = UserUtils.getUser();
			List<String> roleList = user.getRoleIdList();
			Page<BuOrder> page = null;
			String roleId = String.valueOf(roleList.get(0));
			List<String> orderStateList = new ArrayList<>();
			if (StringUtils.isNoneBlank(buOrder.getOrderState())) {
				if (buOrder.getOrderState().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					buOrder.setOrderState("");
				} else {
					buOrder.setOrderState(buOrder.getOrderState().split(",")[0]);
				}
			}

			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getCustomerId())) {
				if (!buOrder.getCustomerId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String customerId = buOrder.getCustomerId();
					User customerSelUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(customerId);
					customerSelUser.setIdList(idList);
					customerSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> customerSelList = userService.findRef(customerSelUser);
					buOrder.setCustomerName(customerSelList.get(0).getName());
				}
			}
			if (StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				if (!buOrder.getSalesmanId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					BuSalesman salesMan = buSalesManService.get(buOrder.getSalesmanId());
					buOrder.setSalesManName(salesMan.getSalesmanName());
				}
			}
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSupplierId())) {
				if (!buOrder.getSupplierId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String supplierId = buOrder.getSupplierId();
					User supplieSelrUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(supplierId);
					supplieSelrUser.setIdList(idList);
					supplieSelrUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> supplierSelList = userService.findRef(supplieSelrUser);
					buOrder.setSupplierName(supplierSelList.get(0).getName());
				}
			}

			List<User> supplierList = Lists.newArrayList();
			List<User> customerList = Lists.newArrayList();
			User customerUser = new User();
			Role customerRole = new Role();
			User supplierUser = new User();
			Role supplierRole = new Role();
			
			List<User> supSelList = Lists.newArrayList();
			List<User> cusSelList = Lists.newArrayList();
			User cusSelUser = new User();
			Role cusSelRole = new Role();
			User supSelUser = new User();
			Role supSelRole = new Role();
			
			TotalEntity entity = new TotalEntity();

			List<String> customerIdList = new ArrayList<>();
			List<String> supplierIdList = new ArrayList<>();
			
			List<String> cusSelIdList = Lists.newArrayList();
			List<String> supSelIdList = Lists.newArrayList();
			
			buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
			buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
			
			String cusVal = "-1";
			String supVal = "-1";

			if (roleId.equals(BusinessConstant.CUSTOMER_ROLE_ID)) { // 业务员角色ID

				BuOrder bc = new BuOrder();
				bc.setCustomerId(user.getId());
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCustomerId(bc);

				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCustomerId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && (BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buCu = new BuOrder();
				buCu.setCustomerId(user.getId());
				buCu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buCu.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buCu.setSupplierId(buOrder.getSupplierId());
				}

				buCu.setOrderState(buOrder.getOrderState());
				buCu.setProductName(buOrder.getProductName());
				buCu.setShipDate(buOrder.getShipDate());
				buCu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCu.setEndOrderDate(buOrder.getEndOrderDate());
                buCu.setBeginShipDate(buOrder.getBeginShipDate());
				buCu.setEndShipDate(buOrder.getEndShipDate());
				buCu.setProductNo(buOrder.getProductNo());
				buCu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCustomerById(buCu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.customerOrderStateList();
				}
				buOrder.setCustomerId(user.getId());
				buOrder.setOrderStateList(orderStateList);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findCustomerPage(new Page<BuOrder>(request, response), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_002)) { // 厂商角色ID

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());		
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buSu.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					  buSu.setCustomerId(buOrder.getCustomerId());  
				}

				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierPage(new Page<BuOrder>(request, response), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_001)) {

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
						
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buSu.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					   buSu.setCustomerId(buOrder.getCustomerId());  
				}

				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierPage(new Page<BuOrder>(request, response), buOrder);
			} else if (roleId.equals(BusinessConstant.GENDAN_ROLE_ID)) { // 跟单角色ID

				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setCreateBy(user);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCreateId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCreateId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}

				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}
				
				BuOrder buCr = new BuOrder();
				buCr.setCreateBy(user);
				buCr.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCr.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buCr.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					   buCr.setCustomerId(buOrder.getCustomerId());  
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					  buCr.setSupplierId(buOrder.getSupplierId());
				}
				
				buCr.setOrderState(buOrder.getOrderState());
				buCr.setProductName(buOrder.getProductName());
				buCr.setShipDate(buOrder.getShipDate());
				buCr.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCr.setEndOrderDate(buOrder.getEndOrderDate());

				buCr.setBeginShipDate(buOrder.getBeginShipDate());
				buCr.setEndShipDate(buOrder.getEndShipDate());
				buCr.setProductNo(buOrder.getProductNo());
				buCr.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCreateById(buCr);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.gendanOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setCreateBy(user);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findPage(new Page<BuOrder>(request, response), buOrder);
			} else { // 管理员角色

				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListForManage(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierIdListForManage(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && customerIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !buOrder.getCustomerId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !buOrder.getSupplierId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buMa = new BuOrder();
				buMa.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buMa.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buMa.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buMa.setCustomerId(buOrder.getCustomerId());	
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buMa.setSupplierId(buOrder.getSupplierId());
				}

				buMa.setOrderState(buOrder.getOrderState());
				buMa.setProductName(buOrder.getProductName());
				buMa.setShipDate(buOrder.getShipDate());
				buMa.setBeginOrderDate(buOrder.getBeginOrderDate());
				buMa.setEndOrderDate(buOrder.getEndOrderDate());

				buMa.setBeginShipDate(buOrder.getBeginShipDate());
				buMa.setEndShipDate(buOrder.getEndShipDate());
				buMa.setProductNo(buOrder.getProductNo());
				buMa.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findAllTotal(buMa);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.adminOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findPage(new Page<BuOrder>(request, response), buOrder);
			}

			Integer ors = -1;
			if (org.apache.commons.lang3.StringUtils.isBlank(buOrder.getOrderState())) {
				ors = -1;
			} else {
				ors = Integer.parseInt(buOrder.getOrderState());
			}

			String salVal = "";
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				salVal = buOrder.getSalesmanId();
			}

			model.addAttribute("supVal", supVal);
			model.addAttribute("salVal", salVal);
			model.addAttribute("cusVal", cusVal);
			model.addAttribute("orderState", ors);

			model.addAttribute("roleId", roleId);
			model.addAttribute("page", page);
			model.addAttribute("supplierList", supplierList);
			model.addAttribute("customerList", customerList);
			model.addAttribute("entity", entity);

			model.addAttribute("picPrefix", picPrefix);
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "查询订单数据失败！失败信息："+e.getMessage());
		}
		return "modules/bu/buExternalOrderList";
	}
	
	@RequestMapping(value = "selectSalesList")
	@ResponseBody
	public Map<String, Object> selectSalesList(BuOrder order, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String customerId = request.getParameter("id");
		BuSalesman salesman = new BuSalesman();
		salesman.setCustomerId(customerId);
		List<BuSalesman> entityList = Lists.newArrayList();
		if (customerId != null && !("").equals(customerId) && !("-1").equals(customerId)) {
			entityList = buSalesManService.findList(salesman);
		}else{
			User user = UserUtils.getUser();
			salesman.setCustomerId(user.getId());
			entityList = buSalesManService.findList(salesman);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("salesmanId", entityList);
		return map;

	}
	
	@RequestMapping(value = "selectButtonSalesList")
	@ResponseBody
	public Map<String, Object> selectButtonSalesList(BuOrder order, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		
		String customerId = request.getParameter("id");
		String orderId = request.getParameter("orderId");
		BuSalesman salesman = new BuSalesman();
		
		List<BuSalesman> entityList = Lists.newArrayList();
			if ((customerId != null && !("").equals(customerId) && !("-1").equals(customerId)) && (orderId != null && !("").equals(orderId))) {
				order.setId(orderId);
				order.setCustomerId(customerId);
				BuOrder orderEntity = buOrderService.findById(order);
				salesman.setId(orderEntity.getSalesmanId());
				BuSalesman buSalesman = buSalesManService.get(salesman);
				if(buSalesman != null){
					buSalesman.setSalesmanName(orderEntity.getSalesManName());
					entityList.add(buSalesman);
				}else{
					salesman.setCustomerId(customerId);
					entityList = buSalesManService.findList(salesman);
				}
			}else{
				salesman.setCustomerId(customerId);
				entityList = buSalesManService.findList(salesman);
			}
		Map<String, Object> map = new HashMap<>();
		map.put("salesmanId", entityList);
		return map;

	}
	
	@RequestMapping(value = "selectButtonSalesListOfBy")
	@ResponseBody
	public Map<String, Object> selectButtonSalesListOfBy(BuOrder order, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		
		String orderId = request.getParameter("orderId");
		String customerId = request.getParameter("id");
		BuSalesman salesmanIsRef = new BuSalesman();
		
		List<BuSalesman> entityListIsRef = Lists.newArrayList();
			if ((customerId != null && !("").equals(customerId) && !("-1").equals(customerId)) && (orderId != null && !("").equals(orderId))) {
				BuOrder orderEntity = buOrderService.get(orderId);
				salesmanIsRef.setId(orderEntity.getSalesmanId());
				salesmanIsRef.setSalesmanName(orderEntity.getSalesManName());
				entityListIsRef.add(salesmanIsRef);
			}
		BuSalesman salesman = new BuSalesman();
		List<BuSalesman> entityList = Lists.newArrayList();
			if (customerId != null && !("").equals(customerId) && !("-1").equals(customerId)) {
				salesman.setCustomerId(customerId);
				entityList = buSalesManService.findList(salesman);
			}
		
		for(BuSalesman salesmanAll: entityList){
			for(BuSalesman salesIsRef: entityListIsRef){
				if(salesmanAll.getId().equals(salesIsRef.getId())){
					salesmanAll.setIsRef(1);   // 默认选中标识
				}else{
					salesmanAll.setIsRef(0);
				}
			}
		}
			
		Map<String, Object> map = new HashMap<>();
		map.put("salesmanId", entityList);
		return map;

	}
	
	@RequestMapping(value = "changSupplier")
	@ResponseBody
	public Map<String, Object> changSupplier(HttpServletRequest request, HttpServletResponse response,
			Model model) {
		
		String id = request.getParameter("id");
		BuOrder returnOrder = buOrderService.get(id);
		User supplierUserIsRef = new User();
		supplierUserIsRef.setId(returnOrder.getSupplierId());
		supplierUserIsRef.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		List<User> supplierListIsRef = userService.findRef(supplierUserIsRef);
		
		User supplierUserIsNotRef = new User();
		Role supplierRoleIsNotRef = new Role();
		supplierRoleIsNotRef.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
		supplierRoleIsNotRef.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		supplierUserIsNotRef.setRole(supplierRoleIsNotRef);
		
		List<User> supplierListAll = userService.findRefRole(supplierUserIsNotRef);
		for(User userAll: supplierListAll){
			for(User userIsRef: supplierListIsRef){
				if(userAll.getId().equals(userIsRef.getId())){
					userAll.setIsRef(1); // 下拉默认选中标示
				}else{
					userAll.setIsRef(0); // 不拉不默认选中
				}
			}
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("supplierList", supplierListAll);
		return map;

	}
	
	@RequestMapping(value = "selectSalesById")
	@ResponseBody
	public Map<String, Object> selectSalesById(BuOrder order, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String id = request.getParameter("id");
		BuSalesman salesman = new BuSalesman();
		salesman.setId(id);
		List<BuSalesman> entityList = Lists.newArrayList();
		if (id != null && !("").equals(id)) {
			entityList.add(buSalesManService.get(salesman));
		}
		Map<String, Object> map = new HashMap<>();
		map.put("salesmanId", entityList);
		return map;

	}

	@RequiresPermissions("bu:buExternalOrder:operate:delivery")
	@RequestMapping(value = "selectById")
	public String selectById(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		String id = request.getParameter("id");
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		String orderState = request.getParameter("orderState");
		model.addAttribute("id", id);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		model.addAttribute("orderState", orderState);
		return "modules/bu/buExternalOrderDelivery";
	}

	@RequiresPermissions("bu:buExternalOrder:operate:update")
	@RequestMapping(value = "updateDelivery")
	public String updateDelivery(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		String id = request.getParameter("id") == null ? "" : request.getParameter("id");
		String oldOrderState = request.getParameter("oldOrderState") == null ? "" : request.getParameter("oldOrderState");
		User user = UserUtils.getUser();
		
		buOrder.setId(id);
		buOrder = buOrderService.findById(buOrder);
		if(StringUtils.isBlank(oldOrderState)){
			oldOrderState = buOrder.getOrderState();
		}
		if(oldOrderState.equals(buOrder.getOrderState())){
			if (!beanValidator(model, buOrder)) {
				return form(request, response, buOrder, model);
			}
			if (buOrder.getOrderState().equals(BusinessConstant.ORDER_STATE_1000)) {
				buOrder.setOrderState(BusinessConstant.ORDER_STATE_1001);
			} else if (buOrder.getOrderState().equals(BusinessConstant.ORDER_STATE_1001)) {
				buOrder.setOrderState(BusinessConstant.ORDER_STATE_1002);
			} else if (buOrder.getOrderState().equals(BusinessConstant.ORDER_STATE_1002)) {
				buOrder.setOrderState(BusinessConstant.ORDER_STATE_1003);
			} else if (buOrder.getOrderState().equals(BusinessConstant.ORDER_STATE_1003)) {
				buOrder.setOrderState(BusinessConstant.ORDER_STATE_1004);
			} else if (buOrder.getOrderState().equals(BusinessConstant.ORDER_STATE_1004)) {
				buOrder.setOrderState(BusinessConstant.ORDER_STATE_1005);
			} else if (buOrder.getOrderState().equals(BusinessConstant.ORDER_STATE_1005)) {
				buOrder.setOrderState(BusinessConstant.ORDER_STATE_1006);
			}
			buOrder.setId(id);
			buOrder.setUpdateBy(user);
			buOrder.setUpdateDate(new Date());
			buOrderService.updateDelivery(buOrder);
		}

		
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";

	}

	@RequiresPermissions("bu:buExternalOrder:operate:update")
	@RequestMapping(value = "ajaxUpdateDelivery")
	@ResponseBody
	public String ajaxUpdateDelivery(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {

		JSONObject json = new JSONObject();
		try{
			String flag = request.getParameter("flag") == null ? "" : request.getParameter("flag");
			String write = request.getParameter("write") == null ? "" : request.getParameter("write");
			String id = request.getParameter("id") == null ? "" : request.getParameter("id");
			String oldOrderState = request.getParameter("oldOrderState") == null ? "" : request.getParameter("oldOrderState");
			User user = UserUtils.getUser();
			buOrder.setId(id);
			buOrder = buOrderService.findById(buOrder);
			int oldOrderStateInt = Integer.valueOf(buOrder.getOrderState());
			int currentOrderStateInt = Integer.valueOf(oldOrderState);
		
			ParamWrapper wrapper = new ParamWrapper(oldOrderStateInt,currentOrderStateInt,user,id,buOrder);
			update(wrapper, model, request, response);
			
			json.put("flag", flag);
			json.put("write", write);
			json.put("msg", ResultEnum.SUCCESS.getMsg());
			json.put("code", ResultEnum.SUCCESS.getCode());
		}catch(Exception e){
			e.printStackTrace();
			json.put("msg", ResultEnum.FAIL.getMsg());
			json.put("code", ResultEnum.FAIL.getCode());
		}
		return json.toString();

	}

	private synchronized int update(ParamWrapper paramWrapper,Model model,HttpServletRequest request,HttpServletResponse response){
		int result = 0;
		int nextOrderState = getNext(paramWrapper.getOldOrderStateInt());
		boolean matchFlag = isMatch(nextOrderState,paramWrapper.getCurrentOrderStateInt());
		
		if(!matchFlag){
			if (paramWrapper.getBuOrder().getOrderState().equals(BusinessConstant.ORDER_STATE_1000)) {
				paramWrapper.getBuOrder().setOrderState(BusinessConstant.ORDER_STATE_1001);
			} else if (paramWrapper.getBuOrder().getOrderState().equals(BusinessConstant.ORDER_STATE_1001)) {
				paramWrapper.getBuOrder().setOrderState(BusinessConstant.ORDER_STATE_1002);
			} else if (paramWrapper.getBuOrder().getOrderState().equals(BusinessConstant.ORDER_STATE_1002)) {
				paramWrapper.getBuOrder().setOrderState(BusinessConstant.ORDER_STATE_1003);
			} else if (paramWrapper.getBuOrder().getOrderState().equals(BusinessConstant.ORDER_STATE_1003)) {
				paramWrapper.getBuOrder().setOrderState(BusinessConstant.ORDER_STATE_1004);
			} else if (paramWrapper.getBuOrder().getOrderState().equals(BusinessConstant.ORDER_STATE_1004)) {
				paramWrapper.getBuOrder().setOrderState(BusinessConstant.ORDER_STATE_1005);
			} else if (paramWrapper.getBuOrder().getOrderState().equals(BusinessConstant.ORDER_STATE_1005)) {
				paramWrapper.getBuOrder().setOrderState(BusinessConstant.ORDER_STATE_1006);
			}
			paramWrapper.getBuOrder().setId(paramWrapper.getId());
			paramWrapper.getBuOrder().setUpdateBy(paramWrapper.getUser());
			paramWrapper.getBuOrder().setUpdateDate(new Date());
			result = buOrderService.updateDelivery(paramWrapper.getBuOrder());	
		}
		return result;
	}
	
	private static int getNext(int oldOrderStateInt){
		int isMatch = 0;
		for(int i = 0;i<OrderStateEnum.values().length;i++){
				if(OrderStateEnum.values()[i].getCode() == oldOrderStateInt){
						isMatch = OrderStateEnum.values()[i+1].getCode();
				}
		}
		return isMatch;
	}
	
	private static boolean isMatch(int nextOrderState,int currentOrderState){
		boolean flag = true;
		if(nextOrderState != currentOrderState){
			flag = false;
		}
		return flag;
	}
	
	@RequestMapping(value = "ajaxCheckOrderState")
	@ResponseBody
	public String ajaxCheckOrderState(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {
		
		JSONObject json = new JSONObject();
		try{
			String flag = request.getParameter("flag") == null ? "" : request.getParameter("flag");
			String write = request.getParameter("write") == null ? "" : request.getParameter("write");
			String id = request.getParameter("id") == null ? "" : request.getParameter("id");
			String oldOrderState = request.getParameter("oldOrderState") == null ? "" : request.getParameter("oldOrderState");
			buOrder.setId(id);
			buOrder = buOrderService.findById(buOrder);
			
			if(oldOrderState.equals(buOrder.getOrderState())){
				json.put("msg", ResultEnum.SUCCESS.getMsg());
				json.put("code", ResultEnum.SUCCESS.getCode());
			}else{
				json.put("msg", ResultEnum.FAIL.getMsg());
				json.put("code", ResultEnum.FAIL.getCode());
			}
			json.put("flag", flag);
			json.put("write", write);
		}catch(Exception e){
			json.put("msg", ResultEnum.FAIL.getMsg());
			json.put("code", ResultEnum.FAIL.getCode());
			e.printStackTrace();
		}
		return json.toString();

	}
	
	/**
	 * 跟单、厂商、业务员角色用户订单修改操作权限
	 * 
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:toUpdate")
	@RequestMapping(value = "toUpdate")
	public String toUpdate(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		
		User customerUser = new User();
		customerUser.setId(buOrder.getCustomerId());
		customerUser.setName(buOrder.getCustomerName());

		User supplierUser = new User();
		supplierUser.setId(buOrder.getSupplierId());
		supplierUser.setName(buOrder.getSupplierName());

		List<User> supplierList = Lists.newArrayList();
		List<User> customerList = Lists.newArrayList();
		
		supplierList.add(supplierUser);
		customerList.add(customerUser);

		BuSalesman salesman = new BuSalesman();
		salesman.setId(buOrder.getSalesmanId());
		salesman.setSalesmanName(buOrder.getSalesManName());
		List<BuSalesman> entityList = Lists.newArrayList();
		entityList.add(salesman);

		model.addAttribute("entityList", entityList);
		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		model.addAttribute("buOrder", buOrder);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		model.addAttribute("picPrefix", picPrefix);
		return "modules/bu/buExternalOrderEdit";

	}

	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = "form")
	public String form(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag") == null ? "" : request.getParameter("flag");
		String write = request.getParameter("write");
		String orderNo = DateUtil.getDate();
		buOrder.setOrderNo(orderNo);
		User user = UserUtils.getUser();

		User customerUser = new User();
		Role customerRole = new Role();
		customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
		customerUser.setRole(customerRole);
		customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		User supplierUser = new User();
		Role supplierRole = new Role();
		supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
		supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		supplierUser.setRole(supplierRole);

		List<User> supplierList = userService.findRefRole(supplierUser);
		List<User> customerList = userService.findRefRole(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		buOrder.setOrderDate(format.format(new Date()));
		buOrder.setCreateUser(user.getName());
		model.addAttribute("buOrder", buOrder);
		model.addAttribute("write", write);
		model.addAttribute("flag", flag);
		return "modules/bu/buExternalOrderForm";

	}

	/**
	 * 查看
	 * 
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = "view")
	public String view(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model) {

		User customerUser = new User();
		customerUser.setId(buOrder.getCustomerId());
		customerUser.setName(buOrder.getCustomerName());

		User supplierUser = new User();
		supplierUser.setId(buOrder.getSupplierId());
		supplierUser.setName(buOrder.getSupplierName());

		List<User> supplierList = Lists.newArrayList();
		List<User> customerList = Lists.newArrayList();
		
		supplierList.add(supplierUser);
		customerList.add(customerUser);

		BuSalesman salesman = new BuSalesman();
		salesman.setId(buOrder.getSalesmanId());
		salesman.setSalesmanName(buOrder.getSalesManName());
		List<BuSalesman> entityList = Lists.newArrayList();
		entityList.add(salesman);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		model.addAttribute("entityList", entityList);
		model.addAttribute("picPrefix", picPrefix);

		model.addAttribute("buOrder", buOrder);
		return "modules/bu/buExternalOrderView";

	}

	/**
	 * 新增数据保存
	 * 
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {

		if (!beanValidator(model, buOrder)) {
			return form(request, response, buOrder, model);
		}
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		User user = UserUtils.getUser();

		Office office = user.getOffice();
		buOrder.setOffice(office);
		String siteId = Site.getCurrentSiteId();
		Site site = new Site();
		site.setId(siteId);
		buOrder.setSite(site);
		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();

		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		buOrder.setSupplierName(supplierList.get(0).getName());
		buOrder.setCustomerName(customerList.get(0).getName());

		BuSalesman salesMan = buSalesManService.get(buOrder.getSalesmanId());
		buOrder.setSalesManName(salesMan.getSalesmanName());

		buOrder.setCreateBy(user);
		buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		buOrder.setOrderState(BusinessConstant.ORDER_STATE_1000);
		buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
		buOrder.setIsAdd(BusinessConstant.IS_ADD);
		
		buOrderService.save(buOrder);

		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";

	}

	/**
	 * 修改数据保存
	 * 
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:edit")
	@RequestMapping(value = "update")
	public String update(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {

		if (!beanValidator(model, buOrder)) {
			return form(request, response, buOrder, model);
		}
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		User user = UserUtils.getUser();

		Office office = user.getOffice();
		buOrder.setOffice(office);
		String siteId = Site.getCurrentSiteId();
		Site site = new Site();
		site.setId(siteId);
		buOrder.setSite(site);
		String id = "";
		if(buOrder.getId().contains(",")){
			id = buOrder.getId().split(",")[0];
			buOrder.setId(id);
		}else{
			id = buOrder.getId();
			buOrder.setId(id);
		}
		
		BuOrder returnOrder = buOrderService.get(id);
		buOrder.setOrderState(returnOrder.getOrderState());
		
		if(buOrder.getSalesmanId() != returnOrder.getSalesmanId()){
			BuSalesman saleEntity = buSalesManService.get(buOrder.getSalesmanId());
			buOrder.setSalesManName(saleEntity.getSalesmanName());
			buOrder.setSalesmanId(saleEntity.getId());
		}
		
		if(buOrder.getCustomerId() != returnOrder.getCustomerId()){
			User customerEntity = userService.get(buOrder.getCustomerId());
			buOrder.setCustomerId(customerEntity.getId());
			buOrder.setCustomerName(customerEntity.getName());
		}
		
		if(buOrder.getSupplierId() != returnOrder.getSupplierId()){
			User supplierEntity = userService.get(buOrder.getSupplierId());
			buOrder.setSupplierId(supplierEntity.getId());
			buOrder.setSupplierName(supplierEntity.getName());
		}

		buOrder.setCreateBy(user);
		buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		buOrder.setOrderState(BusinessConstant.ORDER_STATE_1000);
		buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
		buOrder.setId(id);
		buOrderService.save(buOrder);

		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";

	}
	
	/**
	 * 追加数据保存
	 * 
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:edit")
	@RequestMapping(value = "copySave")
	public String copySave(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {

		if (!beanValidator(model, buOrder)) {
			return form(request, response, buOrder, model);
		}
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		User user = UserUtils.getUser();

		Office office = user.getOffice();
		buOrder.setOffice(office);
		String siteId = Site.getCurrentSiteId();
		Site site = new Site();
		site.setId(siteId);
		buOrder.setSite(site);

		String id = "";
		if(StringUtils.isNotEmpty(buOrder.getId())){
			if(buOrder.getId().contains(",")){
				id = buOrder.getId().split(",")[0];
				buOrder.setId(id);
			}else{
				id = buOrder.getId();
				buOrder.setId(id);
			}
		}
		BuOrder returnOrder = buOrderService.get(id);
		buOrder.setOrderState(returnOrder.getOrderState());
		
		if(buOrder.getSalesmanId() != returnOrder.getSalesmanId()){
			BuSalesman saleEntity = buSalesManService.get(buOrder.getSalesmanId());
			buOrder.setSalesManName(saleEntity.getSalesmanName());
			buOrder.setSalesmanId(returnOrder.getSalesmanId());
		}
		
		if(buOrder.getCustomerId() != returnOrder.getCustomerId()){
			User customerEntity = userService.get(buOrder.getCustomerId());
			buOrder.setCustomerId(customerEntity.getId());
			buOrder.setCustomerName(customerEntity.getName());
		}
		
		if(buOrder.getSupplierId() != returnOrder.getSupplierId()){
			User supplierEntity = userService.get(buOrder.getSupplierId());
			buOrder.setSupplierId(supplierEntity.getId());
			buOrder.setSupplierName(supplierEntity.getName());
		}

		buOrder.setCreateBy(user);
		buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		buOrder.setOrderState(BusinessConstant.ORDER_STATE_1000);
		buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
		buOrder.setIsNewRecord(true);
		buOrder.setId(IdGen.uuid());
		
		buOrder.setIsAdd(BusinessConstant.NOT_ADD);
		
		buOrderService.save(buOrder);

		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";

	}

	protected BuOrderCp constructBuOrderCpEntity(BuOrder buOrder) {

		BuOrderCp orderCp = new BuOrderCp();

		orderCp.setOrderNo(buOrder.getOrderNo());
		orderCp.setProductNo(buOrder.getProductNo());
		orderCp.setOrderDate(buOrder.getOrderDate());
		orderCp.setShipDate(buOrder.getShipDate());
		orderCp.setProductName(buOrder.getProductName());

		orderCp.setCrafts(buOrder.getCrafts());
		orderCp.setElectroplate(buOrder.getElectroplate());
		orderCp.setBack(buOrder.getBack());
		orderCp.setPackages(buOrder.getPackages());
		orderCp.setFitting(buOrder.getFitting());

		orderCp.setPicUrl(buOrder.getPicUrl());
		orderCp.setQuantity(buOrder.getQuantity());
		orderCp.setSalesPrice(buOrder.getSalesPrice());
		orderCp.setSalesModelFee(buOrder.getSalesModelFee());
		orderCp.setProduceFee(buOrder.getProduceFee());

		orderCp.setProduceModelFee(buOrder.getProduceModelFee());
		orderCp.setSalesTotalFee(buOrder.getSalesTotalFee());
		orderCp.setProduceTotalFee(buOrder.getProduceTotalFee());

		return orderCp;

	}

	/**
	 * 订单删除
	 * 
	 * @param buOrder
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:delete")
	@RequestMapping(value = "delete")
	public String delete(BuOrder buOrder, RedirectAttributes redirectAttributes) {
		buOrderService.delete(buOrder);
		addMessage(redirectAttributes, "删除订单成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";
	}
	/**
	 * 订单删除
	 * 
	 * @param buOrder
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:delete")
	@RequestMapping(value = "ajaxDelete")
	@ResponseBody
	public String ajaxDelete(BuOrder buOrder) {
		buOrderService.delete(buOrder);
		JSONObject json = new JSONObject();
		json.put("msg", "删除订单成功");
		json.put("code", 222);
		return json.toString();
	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:copy")
	@RequestMapping(value = "copy")
	public String copy(HttpServletRequest request, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");

		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();

		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		buOrder.setOrderDate(format.format(new Date()));

		BuSalesman salesman = new BuSalesman();
		salesman.setId(buOrder.getSalesmanId());
		List<BuSalesman> entityList = buSalesManService.findList(salesman);

		model.addAttribute("entityList", entityList);

		model.addAttribute("buOrder", buOrder);
		model.addAttribute("picPrefix", picPrefix);
		buOrder.setOrderNo(DateUtil.getDate());
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderCopy";
	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:gendanOperate:gendanCopy")
	@RequestMapping(value = "gendanCopy")
	public String gendanCopy(HttpServletRequest request, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");

		String customerId = buOrder.getCustomerId();
		String supplierId = buOrder.getSupplierId();

		User customerUser = new User();
		customerUser.setId(customerId);
		customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		User supplierUser = new User();
		supplierUser.setId(supplierId);
		supplierUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		buOrder.setOrderDate(format.format(new Date()));

		BuSalesman salesman = new BuSalesman();
		salesman.setId(buOrder.getSalesmanId());
		List<BuSalesman> entityList = buSalesManService.findList(salesman);

		model.addAttribute("entityList", entityList);

		model.addAttribute("buOrder", buOrder);
		model.addAttribute("picPrefix", picPrefix);
		buOrder.setOrderNo(DateUtil.getDate());
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderCopy";
	}

	/**
	 * 查询订单状态
	 * 
	 * @return
	 */
	@RequestMapping(value = "orderStatus")
	@ResponseBody
	public Map<String, Object> getOrderStatus(HttpServletRequest request) {

		Map<String, Object> map = new HashMap<>();

		/*
		 * // 订单状态(0:待审核;1:待生产;2:生产中;3:待发货;4:待收货;5:待收款;6:已完成) int
		 * externalPendingReview = 0; // 外部待审核订单状态 int externalToBeProduced = 0;
		 * // 外部待审核订单状态 int externalInProduction = 0; // 外部生产中订单状态 int
		 * externalToBeDelivered = 0; // 外部待发货订单状态 int externalPendingReceipt =
		 * 0; // 外部待收货订单状态 int externalPendingPayment = 0; // 外部待收款订单状态 int
		 * externalCompleted = 0; // 外部已完成订单状态
		 * 
		 * int internalPendingReview = 0; // 内部待审核订单状态 int internalToBeProduced
		 * = 0; // 内部待审核订单状态 int internalInProduction = 0; // 内部生产中订单状态 int
		 * internalToBeDelivered = 0; // 内部待发货订单状态 int internalPendingReceipt =
		 * 0; // 内部待收货订单状态 int internalPendingPayment = 0; // 内部待收款订单状态 int
		 * internalCompleted = 0; // 内部已完成订单状态
		 * 
		 * boolean trueFlag = true; boolean falseFlag = false;
		 * 
		 * int internalOrderType = 0; int externalOrderType = 1;
		 * 
		 * if (trueFlag == true) {
		 * 
		 * BuOrder internalPendingReviewOrder = new BuOrder();
		 * internalPendingReviewOrder.setOrderState("1000");
		 * internalPendingReviewOrder.setOrderType(String.valueOf(
		 * internalOrderType)); internalPendingReview =
		 * buOrderService.selectOrderStatus(internalPendingReviewOrder);
		 * map.put("internalPendingReview", internalPendingReview);
		 * 
		 * BuOrder internalToBeProducedOrder = new BuOrder();
		 * internalToBeProducedOrder.setOrderState("1001");
		 * internalToBeProducedOrder.setOrderType(String.valueOf(
		 * internalOrderType)); internalToBeProduced =
		 * buOrderService.selectOrderStatus(internalToBeProducedOrder);
		 * map.put("internalToBeProduced", internalToBeProduced);
		 * 
		 * BuOrder internalInProductionOrder = new BuOrder();
		 * internalInProductionOrder.setOrderState("1002");
		 * internalInProductionOrder.setOrderType(String.valueOf(
		 * internalOrderType)); internalInProduction =
		 * buOrderService.selectOrderStatus(internalInProductionOrder);
		 * map.put("internalInProduction", internalInProduction);
		 * 
		 * BuOrder internalToBeDeliveredOrder = new BuOrder();
		 * internalToBeDeliveredOrder.setOrderState("1003");
		 * internalToBeDeliveredOrder.setOrderType(String.valueOf(
		 * internalOrderType)); internalToBeDelivered =
		 * buOrderService.selectOrderStatus(internalToBeDeliveredOrder);
		 * map.put("internalToBeDelivered", internalToBeDelivered);
		 * 
		 * BuOrder internalPendingReceiptOrder = new BuOrder();
		 * internalPendingReceiptOrder.setOrderState("1004");
		 * internalPendingReceiptOrder.setOrderType(String.valueOf(
		 * internalOrderType)); internalPendingReceipt =
		 * buOrderService.selectOrderStatus(internalPendingReceiptOrder);
		 * map.put("internalPendingReceipt", internalPendingReceipt);
		 * 
		 * BuOrder internalPendingPaymentOrder = new BuOrder();
		 * internalPendingPaymentOrder.setOrderState(1005);
		 * internalPendingPaymentOrder.setOrderType(String.valueOf(
		 * internalOrderType)); internalPendingPayment =
		 * buOrderService.selectOrderStatus(internalPendingPaymentOrder);
		 * map.put("internalPendingPayment", internalPendingPayment);
		 * 
		 * BuOrder internalCompletedOrder = new BuOrder();
		 * internalCompletedOrder.setOrderState(1006);
		 * internalCompletedOrder.setOrderType(String.valueOf(internalOrderType)
		 * ); internalCompleted =
		 * buOrderService.selectOrderStatus(internalCompletedOrder);
		 * map.put("internalCompleted", internalCompleted);
		 * 
		 * } if (falseFlag == false) {
		 * 
		 * BuOrder externalPendingReviewOrder = new BuOrder();
		 * externalPendingReviewOrder.setOrderState("1000");
		 * externalPendingReviewOrder.setOrderType(String.valueOf(
		 * externalOrderType)); externalPendingReview =
		 * buOrderService.selectOrderStatus(externalPendingReviewOrder);
		 * map.put("externalPendingReview", externalPendingReview);
		 * 
		 * BuOrder externalToBeProducedOrder = new BuOrder();
		 * externalToBeProducedOrder.setOrderState("1001");
		 * externalToBeProducedOrder.setOrderType(String.valueOf(
		 * externalOrderType)); externalToBeProduced =
		 * buOrderService.selectOrderStatus(externalToBeProducedOrder);
		 * map.put("externalToBeProduced", externalToBeProduced);
		 * 
		 * BuOrder externalInProductionOrder = new BuOrder();
		 * externalInProductionOrder.setOrderState("1002");
		 * externalInProductionOrder.setOrderType(String.valueOf(
		 * externalOrderType)); externalInProduction =
		 * buOrderService.selectOrderStatus(externalInProductionOrder);
		 * map.put("externalInProduction", externalInProduction);
		 * 
		 * BuOrder externalToBeDeliveredOrder = new BuOrder();
		 * externalToBeDeliveredOrder.setOrderState("1003");
		 * externalToBeDeliveredOrder.setOrderType(String.valueOf(
		 * externalOrderType)); externalToBeDelivered =
		 * buOrderService.selectOrderStatus(externalToBeDeliveredOrder);
		 * map.put("externalToBeDelivered", externalToBeDelivered);
		 * 
		 * BuOrder externalPendingReceiptOrder = new BuOrder();
		 * externalPendingReceiptOrder.setOrderState("1004");
		 * externalPendingReceiptOrder.setOrderType(String.valueOf(
		 * externalOrderType)); externalPendingReceipt =
		 * buOrderService.selectOrderStatus(externalPendingReceiptOrder);
		 * map.put("externalPendingReceipt", externalPendingReceipt);
		 * 
		 * BuOrder externalPendingPaymentOrder = new BuOrder();
		 * externalPendingPaymentOrder.setOrderState(1005);
		 * externalPendingPaymentOrder.setOrderType(String.valueOf(
		 * externalOrderType)); externalPendingPayment =
		 * buOrderService.selectOrderStatus(externalPendingPaymentOrder);
		 * map.put("externalPendingPayment", externalPendingPayment);
		 * 
		 * BuOrder externalCompletedOrder = new BuOrder();
		 * externalCompletedOrder.setOrderState(1006);
		 * externalCompletedOrder.setOrderType(String.valueOf(externalOrderType)
		 * ); externalCompleted =
		 * buOrderService.selectOrderStatus(externalCompletedOrder);
		 * map.put("externalCompleted", externalCompleted);
		 * 
		 * }
		 */
		return map;
	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos0")
	@RequestMapping(value = "eos0")
	public void eos0(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos1")
	@RequestMapping(value = "eos1")
	public void eos1(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos2")
	@RequestMapping(value = "eos2")
	public void eos2(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos3")
	@RequestMapping(value = "eos3")
	public void eos3(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos4")
	@RequestMapping(value = "eos4")
	public void eos4(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos5")
	@RequestMapping(value = "eos5")
	public void eos5(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:eos8")
	@RequestMapping(value = "eos8")
	public void eos8(HttpServletRequest request, BuOrder buOrder, Model model) {

	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:isSalesSel")
	@RequestMapping(value = "isSalesSel")
	public void salesSel(HttpServletRequest request, BuOrder buOrder, Model model) {

	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:isNotSalesSel")
	@RequestMapping(value = "isNotSalesSel")
	public void isNotSalesSel(HttpServletRequest request, BuOrder buOrder, Model model) {

	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:isSupplierSel")
	@RequestMapping(value = "isSupplierSel")
	public void supplierSel(HttpServletRequest request, BuOrder buOrder, Model model) {

	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:isNotSupplierSel")
	@RequestMapping(value = "isNotSupplierSel")
	public void isNotsupplierSel(HttpServletRequest request, BuOrder buOrder, Model model) {

	}


	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesmanName")
	@RequestMapping(value = "salesmanName")
	public void salesmanName(HttpServletRequest request, BuOrder buOrder, Model model) {

	}
	
	/**
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:add")
	@RequestMapping(value = "addExternalOrder")
	public String addExternalOrder(HttpServletRequest request, BuOrder buOrder, Model model) {

		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		String orderNo = DateUtil.getDate();
		buOrder.setOrderNo(orderNo);
		User user = UserUtils.getUser();
		buOrder.setCreateBy(user);

		User customerUser = new User();
		Role customerRole = new Role();
		customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
		customerUser.setRole(customerRole);
		customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

		User supplierUser = new User();
		Role supplierRole = new Role();
		supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
		supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		supplierUser.setRole(supplierRole);

		List<User> supplierList = userService.findRef(supplierUser);
		List<User> customerList = userService.findRef(customerUser);

		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);

		model.addAttribute("buOrder", buOrder);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderForm";

	}

	/**
	 * 下载生产单
	 * 
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:export")
	@RequestMapping(value = "exportProductFile", method = RequestMethod.GET)
	public String exportProductFile(HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			String id = request.getParameter("id");
			BuOrder buOrder = new BuOrder();
			buOrder.setId(id);
			buOrder = buOrderService.findById(buOrder);
			ExcelTest.reportOrderMergeXls(BusinessConstant.OUT_ORDER_TYPE_INT, buOrder, "xx", "yy", response,
					picPrefix, winUserfilesBasedir, linuxUserfilesBasedir);
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "下载生产单失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/bu/buExternalOrder/list?repage";
	}
	
	/**
	 * 下载生产单
	 * 
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions({"bu:buExternalOrder:operate:export","bu:buExternalOrder:operate:supexport"})
	@RequestMapping(value = "ajaxExportProductFile", method = RequestMethod.GET)
	@ResponseBody
	public String ajaxExportProductFile(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			ExcelTest.reportOrderMergeXls(BusinessConstant.OUT_ORDER_TYPE_INT, buOrder, "xx", "yy", response,
					picPrefix, winUserfilesBasedir, linuxUserfilesBasedir);
			json.put("msg", "下载生产单成功");
			json.put("code", 222);
		} catch (Exception e) {
			json.put("msg", "下载生产单失败");
			json.put("code", 555);
		}
		return json.toString();
	}

	/**
	 * 导出订单数据
	 * 
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:btnExport")
	@RequestMapping(value = "downloadProductFile", method = RequestMethod.POST)
	public String downloadProductFile(@ModelAttribute BuOrder buOrder, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		
		int flag = 0; // 0:厂商;1:其它
		try {
			User user = UserUtils.getUser();
			List<String> roleList = user.getRoleIdList();
			Page<BuOrder> page = null;
			String roleId = String.valueOf(roleList.get(0));
			List<String> orderStateList = new ArrayList<>();
			if (StringUtils.isNoneBlank(buOrder.getOrderState())) {
				if (buOrder.getOrderState().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					buOrder.setOrderState("");
				} else {
					buOrder.setOrderState(buOrder.getOrderState().split(",")[0]);
				}
			}

			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getCustomerId())) {
				if (!buOrder.getCustomerId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String customerId = buOrder.getCustomerId();
					User customerSelUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(customerId);
					customerSelUser.setIdList(idList);
					customerSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> customerSelList = userService.findRef(customerSelUser);
					buOrder.setCustomerName(customerSelList.get(0).getName());
				}
			}
			if (StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				if (!buOrder.getSalesmanId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					BuSalesman salesMan = buSalesManService.get(buOrder.getSalesmanId());
					buOrder.setSalesManName(salesMan.getSalesmanName());
				}
			}
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSupplierId())) {
				if (!buOrder.getSupplierId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String supplierId = buOrder.getSupplierId();
					User supplieSelrUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(supplierId);
					supplieSelrUser.setIdList(idList);
					supplieSelrUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> supplierSelList = userService.findRef(supplieSelrUser);
					buOrder.setSupplierName(supplierSelList.get(0).getName());
				}
			}

			List<User> supplierList = Lists.newArrayList();
			List<User> customerList = Lists.newArrayList();
			User customerUser = new User();
			Role customerRole = new Role();
			User supplierUser = new User();
			Role supplierRole = new Role();
			
			List<User> supSelList = Lists.newArrayList();
			List<User> cusSelList = Lists.newArrayList();
			User cusSelUser = new User();
			Role cusSelRole = new Role();
			User supSelUser = new User();
			Role supSelRole = new Role();
			
			TotalEntity entity = new TotalEntity();

			List<String> customerIdList = new ArrayList<>();
			List<String> supplierIdList = new ArrayList<>();
			
			List<String> cusSelIdList = Lists.newArrayList();
			List<String> supSelIdList = Lists.newArrayList();
			
			buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
			buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
			
			String cusVal = "-1";
			String supVal = "-1";

			if (roleId.equals(BusinessConstant.CUSTOMER_ROLE_ID)) { // 业务员角色ID

				BuOrder bc = new BuOrder();
				bc.setCustomerId(user.getId());
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCustomerId(bc);

				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCustomerId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buCu = new BuOrder();
				buCu.setCustomerId(user.getId());
				buCu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			          buCu.setSalesmanId(buOrder.getSalesmanId());
			      }
			    if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
			          buCu.setSupplierId(buOrder.getSupplierId());
			      }

				buCu.setOrderState(buOrder.getOrderState());
				buCu.setProductName(buOrder.getProductName());
				buCu.setShipDate(buOrder.getShipDate());
				buCu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCu.setEndOrderDate(buOrder.getEndOrderDate());

				buCu.setBeginShipDate(buOrder.getBeginShipDate());
				buCu.setEndShipDate(buOrder.getEndShipDate());
				buCu.setProductNo(buOrder.getProductNo());
				buCu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCustomerById(buCu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.customerOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setCustomerId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findCustomerExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_002)) { // 厂商角色ID

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			          buSu.setSalesmanId(buOrder.getSalesmanId());
			     }
			    if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
			    	  buSu.setCustomerId(buOrder.getCustomerId());  
			    }
				
				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_001)) {

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			          buSu.setSalesmanId(buOrder.getSalesmanId());
			    }
			    if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
			          buSu.setCustomerId(buOrder.getCustomerId());  
			    }

				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());
	
				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else if (roleId.equals(BusinessConstant.GENDAN_ROLE_ID)) { // 跟单角色ID
				flag = 1;
				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setCreateBy(user);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCreateId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCreateId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null ){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}

				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}
				
				BuOrder buCr = new BuOrder();
				buCr.setCreateBy(user);
				buCr.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCr.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buCr.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					  buCr.setCustomerId(buOrder.getCustomerId());  
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					  buCr.setSupplierId(buOrder.getSupplierId());
				}
				
				buCr.setOrderState(buOrder.getOrderState());
				buCr.setProductName(buOrder.getProductName());
				buCr.setShipDate(buOrder.getShipDate());
				buCr.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCr.setEndOrderDate(buOrder.getEndOrderDate());

				buCr.setBeginShipDate(buOrder.getBeginShipDate());
				buCr.setEndShipDate(buOrder.getEndShipDate());
				buCr.setProductNo(buOrder.getProductNo());
				buCr.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCreateById(buCr);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.gendanOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setCreateBy(user);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findCreateByExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else { // 管理员角色
                flag = 1;
				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListForManage(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierIdListForManage(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && customerIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buMa = new BuOrder();
				buMa.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buMa.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buMa.setCustomerId(buOrder.getCustomerId());	
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buMa.setSupplierId(buOrder.getSupplierId());
				}

				buMa.setOrderState(buOrder.getOrderState());
				buMa.setProductName(buOrder.getProductName());
				buMa.setShipDate(buOrder.getShipDate());
				buMa.setBeginOrderDate(buOrder.getBeginOrderDate());
				buMa.setEndOrderDate(buOrder.getEndOrderDate());

				buMa.setBeginShipDate(buOrder.getBeginShipDate());
				buMa.setEndShipDate(buOrder.getEndShipDate());
				buMa.setProductNo(buOrder.getProductNo());
				buMa.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findAllTotal(buMa);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.adminOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findManageExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			}
			String salVal = "";
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				BuSalesman salesMan = new BuSalesman();
				salesMan.setCustomerId(buOrder.getCustomerId());
				List<BuSalesman> salesmans = buSalesManService.findList(salesMan);
				salVal = salesmans.get(0).getId();
			}
			
			if (flag == 0) {
				ExportToExcelUtil.outCs(picPrefix, response, page.getList());
			} else {
				ExportToExcelUtil.out(picPrefix, response, page.getList());
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "导出订单数据失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + adminPath + "/bu/buExternalOrder/list?repage";
	}
		/**
	 * 导出订单数据
	 * 
	 * @param buOrder
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:btnExport")
	@RequestMapping(value = "ajaxDownloadProductFile", method = RequestMethod.POST)
	@ResponseBody
	public void ajaxDownloadProductFile(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		int flag = 0; // 0:厂商;1:其它
		try {
			User user = UserUtils.getUser();
			List<String> roleList = user.getRoleIdList();
			Page<BuOrder> page = null;
			String roleId = String.valueOf(roleList.get(0));
			List<String> orderStateList = new ArrayList<>();
			if (StringUtils.isNoneBlank(buOrder.getOrderState())) {
				if (buOrder.getOrderState().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					buOrder.setOrderState("");
				} else {
					buOrder.setOrderState(buOrder.getOrderState().split(",")[0]);
				}
			}

			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getCustomerId())) {
				if (!buOrder.getCustomerId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String customerId = buOrder.getCustomerId();
					User customerSelUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(customerId);
					customerSelUser.setIdList(idList);
					customerSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> customerSelList = userService.findRef(customerSelUser);
					buOrder.setCustomerName(customerSelList.get(0).getName());
				}
			}
			if (StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				if (!buOrder.getSalesmanId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					BuSalesman salesMan = buSalesManService.get(buOrder.getSalesmanId());
					buOrder.setSalesManName(salesMan.getSalesmanName());
				}
			}
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSupplierId())) {
				if (!buOrder.getSupplierId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String supplierId = buOrder.getSupplierId();
					User supplieSelrUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(supplierId);
					supplieSelrUser.setIdList(idList);
					supplieSelrUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> supplierSelList = userService.findRef(supplieSelrUser);
					buOrder.setSupplierName(supplierSelList.get(0).getName());
				}
			}

			List<User> supplierList = Lists.newArrayList();
			List<User> customerList = Lists.newArrayList();
			User customerUser = new User();
			Role customerRole = new Role();
			User supplierUser = new User();
			Role supplierRole = new Role();
			
			List<User> supSelList = Lists.newArrayList();
			List<User> cusSelList = Lists.newArrayList();
			User cusSelUser = new User();
			Role cusSelRole = new Role();
			User supSelUser = new User();
			Role supSelRole = new Role();
			
			TotalEntity entity = new TotalEntity();

			List<String> customerIdList = new ArrayList<>();
			List<String> supplierIdList = new ArrayList<>();
			
			List<String> cusSelIdList = Lists.newArrayList();
			List<String> supSelIdList = Lists.newArrayList();
			
			buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
			buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
			
			String cusVal = "-1";
			String supVal = "-1";

			if (roleId.equals(BusinessConstant.CUSTOMER_ROLE_ID)) { // 业务员角色ID

				BuOrder bc = new BuOrder();
				bc.setCustomerId(user.getId());
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCustomerId(bc);

				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCustomerId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buCu = new BuOrder();
				buCu.setCustomerId(user.getId());
				buCu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			          buCu.setSalesmanId(buOrder.getSalesmanId());
			      }
			    if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
			          buCu.setSupplierId(buOrder.getSupplierId());
			      }

				buCu.setOrderState(buOrder.getOrderState());
				buCu.setProductName(buOrder.getProductName());
				buCu.setShipDate(buOrder.getShipDate());
				buCu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCu.setEndOrderDate(buOrder.getEndOrderDate());

				buCu.setBeginShipDate(buOrder.getBeginShipDate());
				buCu.setEndShipDate(buOrder.getEndShipDate());
				buCu.setProductNo(buOrder.getProductNo());
				buCu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCustomerById(buCu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.customerOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setCustomerId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findCustomerExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_002)) { // 厂商角色ID

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null ){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			          buSu.setSalesmanId(buOrder.getSalesmanId());
			     }
			    if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
			    	  buSu.setCustomerId(buOrder.getCustomerId());  
			    }
				
				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_001)) {

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
						
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			          buSu.setSalesmanId(buOrder.getSalesmanId());
			    }
			    if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
			          buSu.setCustomerId(buOrder.getCustomerId());  
			    }

				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else if (roleId.equals(BusinessConstant.GENDAN_ROLE_ID)) { // 跟单角色ID
				flag = 1;
				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setCreateBy(user);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCreateId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCreateId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}

				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}
				
				BuOrder buCr = new BuOrder();
				buCr.setCreateBy(user);
				buCr.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCr.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buCr.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					  buCr.setCustomerId(buOrder.getCustomerId());  
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					  buCr.setSupplierId(buOrder.getSupplierId());
				}
				
				buCr.setOrderState(buOrder.getOrderState());
				buCr.setProductName(buOrder.getProductName());
				buCr.setShipDate(buOrder.getShipDate());
				buCr.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCr.setEndOrderDate(buOrder.getEndOrderDate());

				buCr.setBeginShipDate(buOrder.getBeginShipDate());
				buCr.setEndShipDate(buOrder.getEndShipDate());
				buCr.setProductNo(buOrder.getProductNo());
				buCr.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCreateById(buCr);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.gendanOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setCreateBy(user);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findCreateByExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			} else { // 管理员角色
                flag = 1;
				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListForManage(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierIdListForManage(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && customerIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buMa = new BuOrder();
				buMa.setDelFlag(BusinessConstant.NOT_DEL_FLAG);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buMa.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buMa.setCustomerId(buOrder.getCustomerId());	
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buMa.setSupplierId(buOrder.getSupplierId());
				}

				buMa.setOrderState(buOrder.getOrderState());
				buMa.setProductName(buOrder.getProductName());
				buMa.setShipDate(buOrder.getShipDate());
				buMa.setBeginOrderDate(buOrder.getBeginOrderDate());
				buMa.setEndOrderDate(buOrder.getEndOrderDate());

				buMa.setBeginShipDate(buOrder.getBeginShipDate());
				buMa.setEndShipDate(buOrder.getEndShipDate());
				buMa.setProductNo(buOrder.getProductNo());
				buMa.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findAllTotal(buMa);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.adminOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findManageExportPage(new Page<BuOrder>(request, response,-1), buOrder);
			}
			String salVal = "";
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				BuSalesman salesMan = new BuSalesman();
				salesMan.setCustomerId(buOrder.getCustomerId());
				List<BuSalesman> salesmans = buSalesManService.findList(salesMan);
				salVal = salesmans.get(0).getId();
			}
			
			if (flag == 0) {
				ExportToExcelUtil.outCs(picPrefix, response, page.getList());
				return ;
			} else {
				ExportToExcelUtil.out(picPrefix, response, page.getList());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 批量收款
	 * 
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:batchReceipt")
	@RequestMapping(value = "batchReceipt")
	public String batchReceipt(HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		String ids = request.getParameter("ids");
		String[] idsArr = ids.split(",");
		List<String> idList = new ArrayList<>();
		for (int i = 0; i < idsArr.length; i++) {
			idList.add(idsArr[i]);
		}
		BuOrder buOrder = new BuOrder();
		buOrder.setOrderState(BusinessConstant.ORDER_STATE_1006);
		User user = UserUtils.getUser();
		buOrder.setUpdateBy(user);
		buOrder.setUpdateDate(new Date());
		buOrder.setIdList(idList);
		buOrderService.updateBatchReceipt(buOrder);
		return "redirect:" + adminPath + "/bu/buExternalOrder/list?repage";
	}

	/**
	 * 批量收款
	 * 
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:batchReceipt")
	@RequestMapping(value = "ajaxBatchReceipt")
	@ResponseBody
	public String ajaxBatchReceipt(HttpServletRequest request, HttpServletResponse response) {
		
		JSONObject json = new JSONObject();
		String ids = request.getParameter("ids");
		String[] idsArr = ids.split(",");
		List<String> idList = new ArrayList<>();
		for (int i = 0; i < idsArr.length; i++) {
			idList.add(idsArr[i]);
		}
		BuOrder buOrder = new BuOrder();
		buOrder.setOrderState(BusinessConstant.ORDER_STATE_1006);
		User user = UserUtils.getUser();
		buOrder.setUpdateBy(user);
		buOrder.setUpdateDate(new Date());
		buOrder.setIdList(idList);
		buOrderService.updateBatchReceipt(buOrder);
		json.put("msg", "批量收款成功");
		json.put("code", 222);
		return json.toString();
	}
	
	/**
	 * 销售总价
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesTotalFee")
	@RequestMapping(value = "salesTotalFee")
	public void salesTotalFee(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 销售单价
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesPrice")
	@RequestMapping(value = "salesPrice")
	public void salesPrice(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 利润
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:profit")
	@RequestMapping(value = "profit")
	public void profit(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 业务员查询条件权限
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:customer")
	@RequestMapping(value = "customer")
	public void searchForCustomer(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 客户查询条件权限
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:sales")
	@RequestMapping(value = "sales")
	public void searchForSales(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 厂商查询条件权限
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:supplier")
	@RequestMapping(value = "supplier")
	public void searchForSupplier(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 销售总价之和权限
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:salesTotal")
	@RequestMapping(value = "salesTotal")
	public void salesTotal(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 工厂总价之和权限
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:produceTotal")
	@RequestMapping(value = "produceTotal")
	public void produceTotal(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 利润之和权限
	 * 
	 * @param request
	 * @param buOrder
	 * @param model
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:profitTotal")
	@RequestMapping(value = "profitTotal")
	public void profitTotal(HttpServletRequest request, BuOrder buOrder, Model model) {

	}

	/**
	 * 管理员角色用户订单修改操作权限
	 * 
	 * @param request
	 * @param response
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:manageToUpdate")
	@RequestMapping(value = "manageToUpdate")
	public String manageToUpdate(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder,
			Model model) {
		
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		
		User customerUser = new User();
		customerUser.setId(buOrder.getCustomerId());
		customerUser.setName(buOrder.getCustomerName());

		User supplierUser = new User();
		supplierUser.setId(buOrder.getSupplierId());
		supplierUser.setName(buOrder.getSupplierName());

		List<User> supplierList = Lists.newArrayList();
		List<User> customerList = Lists.newArrayList();
		
		supplierList.add(supplierUser);
		customerList.add(customerUser);

		BuSalesman salesman = new BuSalesman();
		salesman.setId(buOrder.getSalesmanId());
		salesman.setSalesmanName(buOrder.getSalesManName());
		List<BuSalesman> entityList = Lists.newArrayList();
		entityList.add(salesman);

		model.addAttribute("entityList", entityList);
		model.addAttribute("supplierList", supplierList);
		model.addAttribute("customerList", customerList);
		model.addAttribute("buOrder", buOrder);
		model.addAttribute("picPrefix", picPrefix);
		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		return "modules/bu/buExternalOrderManageEdit";

	}

	@RequiresPermissions("bu:buExternalOrder:operate:supSelOrderState")
	@RequestMapping(value = "supSelOrderState")
	public void supSelOrderState(HttpServletRequest request, HttpServletResponse response) {

	}

	@RequiresPermissions("bu:buExternalOrder:operate:cusSelOrderState")
	@RequestMapping(value = "cusSelOrderState")
	public void cusSelOrderState(HttpServletRequest request, HttpServletResponse response) {

	}

	/**
	 * 管理员数据保存
	 * customer(业务员：属于系统用户) supplier(厂商：属于系统用户) salesman(客户：属于外部用户)
	 * @param request
	 * @param response
	 * @param buOrder
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bu:buExternalOrder:operate:edit")
	@RequestMapping(value = "manageSave")
	public String manageSave(HttpServletRequest request, HttpServletResponse response, BuOrder buOrder, Model model,
			RedirectAttributes redirectAttributes) {

		if (!beanValidator(model, buOrder)) {
			return form(request, response, buOrder, model);
		}
		String flag = request.getParameter("flag");
		String write = request.getParameter("write");
		User user = UserUtils.getUser();

		Office office = user.getOffice();
		buOrder.setOffice(office);
		String siteId = Site.getCurrentSiteId();
		Site site = new Site();
		site.setId(siteId);
		buOrder.setSite(site);

		String id = "";
		if(StringUtils.isNotEmpty(buOrder.getId())){
			if(buOrder.getId().contains(",")){
				id = buOrder.getId().split(",")[0];
				buOrder.setId(id);
			}else{
				id = buOrder.getId();
				buOrder.setId(id);
			}
		}
		
		BuOrder returnOrder = buOrderService.get(id);
		buOrder.setOrderState(returnOrder.getOrderState());
		
		if(buOrder.getSalesmanId() != returnOrder.getSalesmanId()){
			BuSalesman saleEntity = buSalesManService.get(buOrder.getSalesmanId());
			buOrder.setSalesManName(saleEntity.getSalesmanName());
			buOrder.setSalesmanId(saleEntity.getId());
		}
		
		if(buOrder.getCustomerId() != returnOrder.getCustomerId()){
			User customerEntity = userService.get(buOrder.getCustomerId());
			buOrder.setCustomerId(customerEntity.getId());
			buOrder.setCustomerName(customerEntity.getName());
		}
		
		if(buOrder.getSupplierId() != returnOrder.getSupplierId()){
			User supplierEntity = userService.get(buOrder.getSupplierId());
			buOrder.setSupplierId(supplierEntity.getId());
			buOrder.setSupplierName(supplierEntity.getName());
		}

		buOrder.setCreateBy(user);
		buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
		buOrderService.save(buOrder);

		model.addAttribute("flag", flag);
		model.addAttribute("write", write);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:" + Global.getAdminPath() + "/bu/buExternalOrder/?repage";

	}
	
	public BuOrder constructBuOrder(HttpServletRequest request) throws ParseException{
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		BuOrder buOrder = new BuOrder();
		
		String supValStr = request.getParameter("supVal") == null ? "" : request.getParameter("supVal");
		String cusValStr = request.getParameter("cusVal") == null ? "" : request.getParameter("cusVal");
		String orderNoStr = request.getParameter("orderNo") == null ? "" : request.getParameter("orderNo");
		String productNoStr = request.getParameter("productNo") == null ? "" : request.getParameter("productNo");
		
		String beginOrderDateStr = request.getParameter("beginOrderDate") == null ? "" : request.getParameter("beginOrderDate");
		String endOrderDateStr = request.getParameter("endOrderDate") == null ? "" : request.getParameter("endOrderDate");
		String beginShipDateStr = request.getParameter("beginShipDate") == null ? "" : request.getParameter("beginShipDate");
		String endShipDateStr = request.getParameter("endShipDate") == null ? "" : request.getParameter("endShipDate");
		
		String productNameStr = request.getParameter("productName") == null ? "" : request.getParameter("productName");
		String orderStateStr = request.getParameter("orderState") == null ? "" : request.getParameter("orderState");
		
		String customerIdStr = request.getParameter("customerId") == null ? "" : request.getParameter("customerId");
		String salesmanIdStr = request.getParameter("salesmanId") == null ? "" : request.getParameter("salesmanId");
		String supplierIdStr = request.getParameter("supplierId") == null ? "" : request.getParameter("supplierId");
		String pageNoStr = request.getParameter("pageNo") == null ? "" : request.getParameter("pageNo");
		String pageSizeStr = request.getParameter("pageSize") == null ? "" : request.getParameter("pageSize");

		buOrder.setCusVal(cusValStr);
		buOrder.setSupVal(supValStr);
		
		buOrder.setOrderNo(orderNoStr);
		buOrder.setProductNo(productNoStr.trim());
		if(StringUtils.isNotEmpty(beginOrderDateStr)){
			buOrder.setBeginOrderDate(format.parse(beginOrderDateStr));
		}
		if(StringUtils.isNotEmpty(endOrderDateStr)){
			buOrder.setEndOrderDate(format.parse(endOrderDateStr));
		}
		if(StringUtils.isNotEmpty(beginShipDateStr)){
			buOrder.setBeginShipDate(format.parse(beginShipDateStr));
		}
		if(StringUtils.isNotEmpty(endShipDateStr)){
			buOrder.setEndShipDate(format.parse(endShipDateStr));
		}
		
		buOrder.setProductName(productNameStr);
		buOrder.setOrderState(orderStateStr);
		buOrder.setCustomerId(customerIdStr);
		buOrder.setSalesmanId(salesmanIdStr);
		buOrder.setSupplierId(supplierIdStr);
		
		Page<BuOrder> page = new Page<>();
		if(StringUtils.isNotEmpty(pageNoStr)){
			page.setPageNo(Integer.parseInt(String.valueOf(pageNoStr)));
		}
		if(StringUtils.isNotEmpty(pageSizeStr)){
			page.setPageSize(Integer.parseInt(String.valueOf(pageSizeStr)));
		}
		buOrder.setPage(page);
		
		return buOrder;
	}
	
	@RequiresPermissions("bu:buExternalOrder:view")
	@RequestMapping(value = "json")
	@ResponseBody
	public Map<String, Object> json(BuOrder buOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		
         
		// 订单状态(0:待审核;1:待生产;2:生产中;3:待发货;4:待收货;5:待收款;6:已完成)
		Map<String, Object> json = new HashMap<>();
		try {
			buOrder = constructBuOrder(request);
			
			User user = UserUtils.getUser();
			List<String> roleList = user.getRoleIdList();
			Page<BuOrder> page = null;
			String roleId = String.valueOf(roleList.get(0));
			List<String> orderStateList = new ArrayList<>();
			if (StringUtils.isNoneBlank(buOrder.getOrderState())) {
				if (buOrder.getOrderState().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					buOrder.setOrderState("");
				} else {
					buOrder.setOrderState(buOrder.getOrderState().split(",")[0]);
				}
			}

			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getCustomerId())) {
				if (!buOrder.getCustomerId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String customerId = buOrder.getCustomerId();
					User customerSelUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(customerId);
					customerSelUser.setIdList(idList);
					customerSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> customerSelList = userService.findRef(customerSelUser);
					buOrder.setCustomerName(customerSelList.get(0).getName());
				}
			}
			if (StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				if (!buOrder.getSalesmanId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					BuSalesman salesMan = buSalesManService.get(buOrder.getSalesmanId());
					buOrder.setSalesManName(salesMan.getSalesmanName());
				}
			}
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSupplierId())) {
				if (!buOrder.getSupplierId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					String supplierId = buOrder.getSupplierId();
					User supplieSelrUser = new User();
					List<String> idList = new ArrayList<>();
					idList.add(supplierId);
					supplieSelrUser.setIdList(idList);
					supplieSelrUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
					List<User> supplierSelList = userService.findRef(supplieSelrUser);
					buOrder.setSupplierName(supplierSelList.get(0).getName());
				}
			}

			List<User> supplierList = Lists.newArrayList();
			List<User> customerList = Lists.newArrayList();
			User customerUser = new User();
			Role customerRole = new Role();
			User supplierUser = new User();
			Role supplierRole = new Role();
			
			List<User> supSelList = Lists.newArrayList();
			List<User> cusSelList = Lists.newArrayList();
			User cusSelUser = new User();
			Role cusSelRole = new Role();
			User supSelUser = new User();
			Role supSelRole = new Role();
			
			TotalEntity entity = new TotalEntity();

			List<String> customerIdList = new ArrayList<>();
			List<String> supplierIdList = new ArrayList<>();
			
			List<String> cusSelIdList = Lists.newArrayList();
			List<String> supSelIdList = Lists.newArrayList();
			
			buOrder.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
			buOrder.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
			
			String cusVal = "-1";
			String supVal = "-1";

			if (roleId.equals(BusinessConstant.CUSTOMER_ROLE_ID)) { // 业务员角色ID

				BuOrder bc = new BuOrder();
				bc.setCustomerId(user.getId());
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCustomerId(bc);

				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());		
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCustomerId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buCu = new BuOrder();
				buCu.setCustomerId(user.getId());
				buCu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buCu.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buCu.setSupplierId(buOrder.getSupplierId());
				}

				buCu.setOrderState(buOrder.getOrderState());
				buCu.setProductName(buOrder.getProductName());
				buCu.setShipDate(buOrder.getShipDate());
				buCu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCu.setEndOrderDate(buOrder.getEndOrderDate());

				buCu.setBeginShipDate(buOrder.getBeginShipDate());
				buCu.setEndShipDate(buOrder.getEndShipDate());
				buCu.setProductNo(buOrder.getProductNo());
				buCu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCustomerById(buCu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.customerOrderStateList();
				}
				buOrder.setCustomerId(user.getId());
				buOrder.setOrderStateList(orderStateList);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findCustomerPage(new Page<BuOrder>(request, response), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_002)) { // 厂商角色ID

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buSu.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					  buSu.setCustomerId(buOrder.getCustomerId());  
				}

				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());
				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierPage(new Page<BuOrder>(request, response), buOrder);
			} else if (roleId.equals(BusinessConstant.SUPPLIER_ROLE_ID_001)) {

				BuOrder bc = new BuOrder();
				bc.setSupplierId(user.getId());
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListBySupplierId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListBySupplierId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
					
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID);
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);
				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !("-1").equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !("-1").equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buSu = new BuOrder();
				buSu.setSupplierId(user.getId());
				buSu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buSu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buSu.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					   buSu.setCustomerId(buOrder.getCustomerId());  
				}

				buSu.setOrderState(buOrder.getOrderState());
				buSu.setProductName(buOrder.getProductName());
				buSu.setShipDate(buOrder.getShipDate());
				buSu.setBeginOrderDate(buOrder.getBeginOrderDate());
				buSu.setEndOrderDate(buOrder.getEndOrderDate());

				buSu.setBeginShipDate(buOrder.getBeginShipDate());
				buSu.setEndShipDate(buOrder.getEndShipDate());
				buSu.setProductNo(buOrder.getProductNo());
				buSu.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findSupplierById(buSu);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.supplierOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setSupplierId(user.getId());
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = iBuOrderService.findSupplierPage(new Page<BuOrder>(request, response), buOrder);
			} else if (roleId.equals(BusinessConstant.GENDAN_ROLE_ID)) { // 跟单角色ID

				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				bc.setCreateBy(user);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListByCreateId(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());	
						}
					}
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierListByCreateId(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && supplierIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}

				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}
				
				BuOrder buCr = new BuOrder();
				buCr.setCreateBy(user);
				buCr.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buCr.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					   buCr.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					   buCr.setCustomerId(buOrder.getCustomerId());  
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					  buCr.setSupplierId(buOrder.getSupplierId());
				}
				
				buCr.setOrderState(buOrder.getOrderState());
				buCr.setProductName(buOrder.getProductName());
				buCr.setShipDate(buOrder.getShipDate());
				buCr.setBeginOrderDate(buOrder.getBeginOrderDate());
				buCr.setEndOrderDate(buOrder.getEndOrderDate());

				buCr.setBeginShipDate(buOrder.getBeginShipDate());
				buCr.setEndShipDate(buOrder.getEndShipDate());
				buCr.setProductNo(buOrder.getProductNo());
				buCr.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findCreateById(buCr);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.gendanOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				buOrder.setCreateBy(user);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findByGenDanPage(new Page<BuOrder>(request, response), buOrder);
			} else { // 管理员角色

				BuOrder bc = new BuOrder();
				bc.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				bc.setOrderType(BusinessConstant.OUT_ORDER_TYPE);
				List<BuOrder> customerEntity = buOrderService.selectCustomerIdListForManage(bc);
				for (BuOrder bor : customerEntity) {
					if(bor != null){
						if(bor.getCustomerId() != null){
							customerIdList.add(bor.getCustomerId());
						}
					}
					
				}
				List<BuOrder> supplierEntity = buOrderService.selectSupplierIdListForManage(bc);
				for (BuOrder bor : supplierEntity) {
					if(bor != null){
						if(bor.getSupplierId() != null){
							supplierIdList.add(bor.getSupplierId());
						}
					}
				}

				customerRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员角色ID
				customerUser.setRole(customerRole);
				customerUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				customerUser.setIdList(customerIdList);

				supplierRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supplierRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supplierUser.setRole(supplierRole);
				supplierUser.setIdList(supplierIdList);

				if (customerIdList != null && customerIdList.size() > 0) {
					customerList = userService.findRef(customerUser);
				}
				if (supplierIdList != null && customerIdList.size() > 0) {
					supplierList = userService.findRef(supplierUser);
				}
				
				cusSelRole.setId(BusinessConstant.CUSTOMER_ROLE_ID); // 业务员
				cusSelUser.setRole(cusSelRole);
				cusSelUser.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				cusSelUser.setIdList(customerIdList);

				supSelRole.setId(BusinessConstant.SUPPLIER_ROLE_ID_001); // 厂商角色ID
				supSelRole.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				supSelUser.setRole(supSelRole);
				
				if (StringUtils.isNotBlank(buOrder.getCustomerId()) && !buOrder.getCustomerId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					cusSelIdList.add(buOrder.getCustomerId());
					cusSelUser.setIdList(cusSelIdList);
					cusSelList = userService.findRef(cusSelUser);
					cusVal = cusSelList.get(0).getId();
				}
				
				if (StringUtils.isNotBlank(buOrder.getSupplierId()) && !buOrder.getSupplierId().equals(BusinessConstant.SEL_ORDER_STATE_FLAG)) {
					supSelIdList.add(buOrder.getSupplierId());
					supSelUser.setIdList(supSelIdList);
					supSelList = userService.findRef(supSelUser);
					supVal = supSelList.get(0).getId();
				}

				BuOrder buMa = new BuOrder();
				buMa.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
				buMa.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

				if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buMa.setSalesmanId(buOrder.getSalesmanId());
				}
				if(StringUtils.isNotBlank(buOrder.getCustomerId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buMa.setCustomerId(buOrder.getCustomerId());	
				}
				if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buMa.setSupplierId(buOrder.getSupplierId());
				}

				buMa.setOrderState(buOrder.getOrderState());
				buMa.setProductName(buOrder.getProductName());
				buMa.setShipDate(buOrder.getShipDate());
				buMa.setBeginOrderDate(buOrder.getBeginOrderDate());
				buMa.setEndOrderDate(buOrder.getEndOrderDate());
				
				buMa.setEndShipDate(buOrder.getEndShipDate());
				buMa.setBeginShipDate(buOrder.getBeginShipDate());
				buMa.setProductNo(buOrder.getProductNo());
				buMa.setOrderNo(buOrder.getOrderNo());

				entity = buOrderService.findAllTotal(buMa);
				if (StringUtils.isBlank(buOrder.getOrderState())) {
					orderStateList = OrderStateWrapper.adminOrderStateList();
				}
				buOrder.setOrderStateList(orderStateList);
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
					buOrder.setSalesmanId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getCustomerId())){
					buOrder.setCustomerId("");
				}
				if((BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
					buOrder.setSupplierId("");
				}
				page = buOrderService.findByManagePage(new Page<BuOrder>(request, response), buOrder);
			}

			Integer ors = -1;
			if(StringUtils.isNotBlank(buOrder.getOrderState())) {
				ors = Integer.parseInt(buOrder.getOrderState());
			}

			String salVal = "-1";
			if (org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId())) {
				salVal = buOrder.getSalesmanId();
			}

			json.put("supVal", supVal);
			json.put("salVal", salVal);
			json.put("cusVal", cusVal);
			json.put("orderState", ors);
			
			json.put("page", page);
			json.put("supplierList", supplierList);
			json.put("customerList", customerList);
			json.put("entity", entity);
			json.put("resultCode", 222);
			json.put("picPrefix",picPrefix);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("resultCode", 555);
		}
		return json;
	}
	
}